package com.jcl.cloud.services.crawler.catchnew.thread;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jcl.cloud.common.service.util.HttpClientUtil;
import com.jcl.cloud.services.crawler.catchnew.constant.TaskInfo;
import com.jcl.cloud.services.crawler.catchnew.util.FileUtil;
import com.jcl.cloud.services.crawler.catchnew.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.List;
import java.util.Map;

public class QcwyFileThread implements Runnable {
    private final static Logger logger = LoggerFactory.getLogger(QcwyFileThread.class);

    @Override
    public void run() {
        while (true){

            try {
                String fileId = TaskInfo.qcwyFileQueue.poll();
                if(null != fileId){
                  this.extractFile(fileId);
                }else {
                    Thread.sleep(5000);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }




    public void extractFile(String fileId) {

        try {

            File file = new File("/app/jcl-crawler/xls/" + fileId + ".xls");

            //调API上传
            List<Map> infoList = FileUtil.resolveExcel(file);

            for(Map infoMap:infoList){
                try {
                    String retStr = HttpClientUtil.doPostJson("http://www.jucailin.com/api/resume/save", JSON.toJSONString(infoMap));
                    String type = JSON.parseObject(retStr).getString("type");
                    if ("success".equals(type)) {
                        logger.info("上传简历数据成功");
                        String id = JSONObject.parseObject(retStr).getJSONObject("resume").getString("id");
                        logger.info("前程无忧的resume_id = " + id);

                        //上传工作经历
                        List<Map<String, String>> worksList = StringUtil.wd2MapQcwy((String) (infoMap.get("work_detail")));
                        for (int i = 0; i < worksList.size(); i++) {
                            Map workMap = worksList.get(i);
                            workMap.put("resume_id", id);
                            HttpClientUtil.doPostJson("http://www.jucailin.com/api/resume/worksave", JSON.toJSONString(workMap));
                        }


                        //上传教育经历
                        List<Map<String, String>> edusList = StringUtil.edu2MapQcwy((String) (infoMap.get("edu_detail")));
                        for (int i = 0; i < edusList.size(); i++) {
                            Map eduMap = edusList.get(i);
                            eduMap.put("resume_id", id);
                            HttpClientUtil.doPostJson("http://www.jucailin.com/api/resume/edusave", JSON.toJSONString(eduMap));
                        }

                        //上传项目经历
                        List<Map<String, String>> projectsList = StringUtil.pd2MapQcwy((String) (infoMap.get("project_detail")));
                        for (int i = 0; i < projectsList.size(); i++) {
                            Map projectMap = projectsList.get(i);
                            projectMap.put("resume_id", id);
                            HttpClientUtil.doPostJson("http://www.jucailin.com/api/resume/projectsave", JSON.toJSONString(projectMap));
                        }
                    }
                } catch (Exception e) {
                    logger.error("前程无忧网站上传单个简历发生异常：",e);
                }
            }
        } catch (Exception e) {
            logger.error(String.format("解析文件发生异常：%s",e.getMessage()),e);
        }
    }
}
